import os
from common import FileTool


def merge_done_log(path, year, startwith):
    file_list = FileTool.get_file_list('log', dir_path)
    os.chdir(dir_path)
    readlines = []
    for value in file_list:
        path, file_name = os.path.split(value)
        if file_name.startswith(startwith):

            if file_name.find(str(year)) > -1:
                with open(value, encoding='utf-8') as o:
                    readlines.extend(o.readlines())
                os.remove(value)
    if len(readlines) > 0:
        with open(path + '/' + startwith + '-' + str(year) + '.log', 'a+', encoding='utf-8') as f:
            for line in readlines:
                f.write(line)


def merge_done_text(path, year, startwith):
    file_list = FileTool.get_file_list('text', dir_path)
    os.chdir(dir_path)
    readlines = []
    for value in file_list:
        path, file_name = os.path.split(value)
        if file_name.startswith(startwith):

            if file_name.find(str(year)) > -1:
                with open(value, encoding='utf-8') as o:
                    readlines.extend(o.readlines())
                os.remove(value)
    if len(readlines) > 0:
        with open(path + '/' + startwith + '-' + str(year) + '.log', 'a+', encoding='utf-8') as f:
            for line in readlines:
                f.write(line)


cur_dir = os.getcwd()
if __name__ == '__main__':
    dir_path = 'C:\\gitee\\PycharmProjects\\DownPython\\porn\\all\\zpdr_ycsq_all'
    # merge_done_log(dir_path, 2021, 'done')
    merge_done_text(dir_path, 2021, 'Done-All-ZPDR')
    # f.writelines(readlines)
    # print(value)
    # print(file_list)
